package com.ldzy.controller;

import com.github.pagehelper.Page;
import com.ldzy.pojo.Medicine;
import com.ldzy.pojo.Result;
import com.ldzy.pojo.ResultCode;
import com.ldzy.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;


@Controller
public class MedicineController {
    @Autowired
    MedicineService medicineService;
    @RequestMapping("/medical")//查询药品信息
    public ModelAndView selectMedicine(Integer pageNum, Integer pageSize,Medicine m){
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 3;
        Page<Medicine> medicine = medicineService.search(pageNum, pageSize,m);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("medical");
        modelAndView.addObject("pageResult", medicine.getResult());
        modelAndView.addObject("myTotal", medicine.getTotal());
        modelAndView.addObject("myPageSize", medicine.getPageSize());
        modelAndView.addObject("pageNum", medicine.getPageNum());
        modelAndView.addObject("gourl", "/medical");
        modelAndView.addObject("search", m);
        return modelAndView;
    }
    @RequestMapping("/searchDeleteMd")//点击删除信息跳转到删除页面
    public ModelAndView deleteMd(Integer pageNum, Integer pageSize,Medicine m){
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 5;
        Page<Medicine> medicine=medicineService.search(pageNum, pageSize,m);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("staff/deleteMd");
        modelAndView.addObject("view", medicine.getResult());
        modelAndView.addObject("myTotal", medicine.getTotal());
        modelAndView.addObject("myPageSize", medicine.getPageSize());
        modelAndView.addObject("pageNum", medicine.getPageNum());
        modelAndView.addObject("gourl", "/searchDeleteMd");
        return modelAndView;
    }
    @RequestMapping("/deleteByMId")//点击删除按钮根据mid删除药品信息
    @ResponseBody
    public Result<Medicine> delete(int mid){
        int j=medicineService.deleteMd(mid);
        if (j>0){
        return new Result<>(true, ResultCode.SUCCESS_DELECT.getDesc(), ResultCode.SUCCESS_DELECT.getCode());
        }else {
        return new Result<>(false,ResultCode.ERROR_DELECT.getDesc(), ResultCode.ERROR_DELECT.getCode());
        }
    }
    @RequestMapping("/addM")//点击添加药品信息跳转到添加页面
    public String addM(){
        return "/staff/addMedicine";
    }

    @RequestMapping("/addMedicine")//点击添加药品信息跳转到添加页面
    public void addMedicine(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("name");
        String mode=req.getParameter("mode");
        String efficacy=req.getParameter("efficacy");
        int price=Integer.parseInt(req.getParameter("price"));
        int count=Integer.parseInt(req.getParameter("count"));
        if(Objects.equals(name, "") ||name==null|| Objects.equals(mode, "") ||mode==null|| Objects.equals(efficacy, "") ||efficacy==null
        ||Objects.equals(price,"")||Objects.equals(count,"")){
            req.setAttribute("msg","药品名称、功效、用途不能为空");
            req.getRequestDispatcher("admin/error.jsp").forward(req, resp);
            return;
        }
        Medicine medicine=new Medicine();
        medicine.setName(name);
        medicine.setMode(mode);
        medicine.setEfficacy(efficacy);
        medicine.setPrice(price);
        medicine.setCount(count);
        int j=medicineService.addMedicine(medicine);
        if (j>0){
//            req.setAttribute("pageResult",medicine);
//            req.getRequestDispatcher("/medical").forward(req, resp);
            req.setAttribute("msg",name+" 添加成功");
            req.getRequestDispatcher("admin/error.jsp").forward(req, resp);
            return;
        }else {
            req.setAttribute("msg","新增失败");
            req.getRequestDispatcher("admin/error.jsp").forward(req, resp);
            return;
        }
    }
    @RequestMapping("/updateMd")//点击修改药品信息跳转到修改页面
    public String updateMd(){
        return "/staff/addMedicine";
    }
}
